﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using System.Windows.Forms;
using fideeraSoft.Utils;
using System.Data.SqlClient;
using System.Data;

namespace fideeraSoft.Paneles
{
    public class MateriaPrimaPanelModificar:PanelGeneral
    {
        private System.Windows.Forms.Button btBuscarMateriaPrim;
        private System.Windows.Forms.DataGridView dgvMostrarMateriaPrima;
        private System.Windows.Forms.Button btGuardarCambios;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.TextBox tbBuscarMateriaPrima;
        private System.Windows.Forms.Button btVolver;

        int locationTlpBuscarMateriaPrima;
        private TableLayoutPanel tlpBuscarMatPrim;
        int locationBtCambios;
        private  SqlDataAdapter adapter;
        private  DataSet ds;
        private SqlCommandBuilder cmdBuilder;
        private DataSet changes;


        public MateriaPrimaPanelModificar(Handler handler)
        {
            this.formatearPanelGeneral(handler);
            
            InitializeComponent();

            this.formatearTableTitulo("MODIFICACIÓN DE MATERIA PRIMA / INSUMO");
                        
            string query = "SELECT ID AS [ID Materia Prima], nombre AS Nombre, codigo AS Código, descripcion AS Descripción, unidad AS Unidad,caracteristicas AS Características FROM MateriaPrima";

            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables[0].Rows.Count <= 0)
                    MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


                dgvMostrarMateriaPrima.DataSource = ds.Tables[0];


                this.formatearDataGrid();
                this.formatearTablaBuscarMateriaPrima();

                this.formatearTablaBotones();


                this.Controls.Add(dgvMostrarMateriaPrima);
                this.Controls.Add(tlpBuscarMatPrim);
                this.Controls.Add(tlpBotones);
            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }


        }

        #region INICIALIZACION
        private void InitializeComponent()
        {
            this.btBuscarMateriaPrim = new System.Windows.Forms.Button();
            this.dgvMostrarMateriaPrima = new System.Windows.Forms.DataGridView();
            this.btGuardarCambios = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarMateriaPrima = new System.Windows.Forms.TextBox();
            this.btVolver = new System.Windows.Forms.Button();
            this.tlpBuscarMatPrim = new System.Windows.Forms.TableLayoutPanel();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrarMateriaPrima)).BeginInit();
            this.SuspendLayout();
            // 
            // btBuscarMateriaPrim
            // 
            this.btBuscarMateriaPrim.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMateriaPrim.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMateriaPrim.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMateriaPrim.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMateriaPrim.Name = "btBuscarMateriaPrim";
            this.btBuscarMateriaPrim.Size = new System.Drawing.Size(75, 23);
            this.btBuscarMateriaPrim.TabIndex = 0;
            this.btBuscarMateriaPrim.Text = "Buscar Materia Prima (Nombre)";
            this.btBuscarMateriaPrim.UseVisualStyleBackColor = false;
            this.btBuscarMateriaPrim.Click += new System.EventHandler(this.btBuscarMateriaPrima_Click);
            // 
            // dgvMostrarMateriaPrima
            // 
            this.dgvMostrarMateriaPrima.AllowUserToResizeRows = false;
            this.dgvMostrarMateriaPrima.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrarMateriaPrima.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrarMateriaPrima.Name = "dgvMostrarMateriaPrima";
            this.dgvMostrarMateriaPrima.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrarMateriaPrima.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrarMateriaPrima.TabIndex = 0;
            this.dgvMostrarMateriaPrima.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMateriaPrima_CellClick);
            this.dgvMostrarMateriaPrima.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMateriaPrima_CellContentClick);
            // 
            // btGuardarCambios
            // 
            this.btGuardarCambios.BackColor = System.Drawing.Color.AliceBlue;
            this.btGuardarCambios.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btGuardarCambios.Location = new System.Drawing.Point(0, 0);
            this.btGuardarCambios.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btGuardarCambios.Name = "btGuardarCambios";
            this.btGuardarCambios.Size = new System.Drawing.Size(75, 40);
            this.btGuardarCambios.TabIndex = 0;
            this.btGuardarCambios.Text = "Guardar";
            this.btGuardarCambios.UseVisualStyleBackColor = false;
            this.btGuardarCambios.Click += new System.EventHandler(this.btGuardarCambios_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // tbBuscarMateriaPrima
            // 
            this.tbBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMateriaPrima.MaxLength = 50;
            this.tbBuscarMateriaPrima.Name = "tbBuscarMateriaPrima";
            this.tbBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarMateriaPrima.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tlpBuscarMatPrim
            // 
            this.tlpBuscarMatPrim.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMatPrim.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMatPrim.ColumnCount = 2;
            this.tlpBuscarMatPrim.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.Cursor = System.Windows.Forms.Cursors.Default;
            this.tlpBuscarMatPrim.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMatPrim.Name = "tlpBuscarMatPrim";
            this.tlpBuscarMatPrim.RowCount = 1;
            this.tlpBuscarMatPrim.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarMatPrim.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrarMateriaPrima)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        #region FORMATEAR

        private void formatearDataGrid()
        {
            dgvMostrarMateriaPrima.AutoGenerateColumns = true;
            dgvMostrarMateriaPrima.Size = new Size(this.Size.Width - 100, this.Size.Height - 400);
            dgvMostrarMateriaPrima.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrarMateriaPrima.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrarMateriaPrima.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrarMateriaPrima.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrarMateriaPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrarMateriaPrima.Size.Width), this.InitialRealPointVertical);
        }
                
        private void formatearTablaBuscarMateriaPrima()
        {
            tlpBuscarMatPrim.Controls.Add(btBuscarMateriaPrim);
            tlpBuscarMatPrim.Controls.Add(tbBuscarMateriaPrima);
                        
            locationTlpBuscarMateriaPrima = dgvMostrarMateriaPrima.Size.Height + tbBuscarMateriaPrima.Size.Height + this.InitialRealPointVertical;

            tlpBuscarMatPrim.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarMatPrim.Size.Width), locationTlpBuscarMateriaPrima);

            

        }

        private void formatearTablaBotones()
        {

            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBotones.BackColor = Color.LightCyan;

            locationBtCambios = locationTlpBuscarMateriaPrima + tlpBuscarMatPrim.Size.Height + 10;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btGuardarCambios);

        }



        #endregion


        private void btBuscarMateriaPrima_Click(object sender, EventArgs e)
        {
            dgvMostrarMateriaPrima.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarMateriaPrima.Text))
            {
                string searchValue = tbBuscarMateriaPrima.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvMostrarMateriaPrima.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrarMateriaPrima.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMostrarMateriaPrima.CurrentCell = row.Cells[0];
                            tbBuscarMateriaPrima.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarMateriaPrima.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO MATERIA PRIMA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btGuardarCambios_Click(object sender, EventArgs e)
        {
            try
            {
                if (adapter != null)
                {
                    cmdBuilder = new SqlCommandBuilder(adapter);
                    changes = ds.GetChanges();
                    if (changes != null)
                    {
                        adapter.Update(changes);
                        MessageBox.Show("CAMBIOS GUARDADOS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("NO HAY CAMBIOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("ERROR AL GUARDAR LOS DATOS.\nNo se guardaran los datos. \nVerifique celdas modificadas y cantidad de dígitos modificados.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                CLogger.Append("[btGuardarCambios_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                this.handler.backToPrincipal();
            }
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void dgvMostrarMateriaPrima_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //dgvMostrarMateriaPrima.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void dgvMostrarMateriaPrima_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrarMateriaPrima.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }
    }
}
